package com.campus.dao.impl;

import com.campus.dao.StudentDao;
import com.campus.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @desc: 学员数据访问实现类
 * @author: hjm
 * @date: 2024/11/22
 */
@Repository
public class StudentDaoImpl implements StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 新增学员
     *
     * @param student 学员对象
     * @return 受影响的行数
     */
    @Override
    public int createStudent(Student student) {
        String sql = "INSERT INTO t_manager_student (stu_name, from_place, parent_phone, current_grade, is_first, login_id, comment) VALUES (?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, student.getStuName(), student.getFromPlace(), student.getParentPhone(), student.getCurrentGrade(), student.getIsFirst(), student.getLoginId(), student.getComment());
    }

    /**
     * 批量导入学员
     *
     * @param students 学员对象列表
     * @return 受影响的行数
     */
    @Override
    public int batchCreateStudents(List<Student> students) {
        String sql = "INSERT INTO t_manager_student (stu_name, from_place, parent_phone, current_grade, is_first, login_id, comment) VALUES (?, ?, ?, ?, ?, ?, ?)";
        int result = 0;
        for (Student student : students) {
            result += jdbcTemplate.update(sql, student.getStuName(), student.getFromPlace(), student.getParentPhone(), student.getCurrentGrade(), student.getIsFirst(), student.getLoginId(), student.getComment());
        }
        return result;
    }

    /**
     * 审核学员
     *
     * @param id 学员ID
     * @return 受影响的行数
     */
    @Override
    public int approveStudent(int id) {
        String sql = "UPDATE t_manager_student SET comment = 'Approved' WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    /**
     * 批量审核学员
     *
     * @param studentIds 学员ID列表
     * @return 受影响的行数
     */
    @Override
    public int batchApproveStudents(List<Integer> studentIds) {
        String sql = "UPDATE t_manager_student SET comment = 'Approved' WHERE id = ?";
        int result = 0;
        for (Integer id : studentIds) {
            result += jdbcTemplate.update(sql, id);
        }
        return result;
    }
}